package cn.deschen.eshop.user.service.impl;

import cn.deschen.eshop.common.cache.RedisService;
import cn.deschen.eshop.generator.model.UmsAdmin;
import cn.deschen.eshop.generator.model.UmsResource;
import cn.deschen.eshop.user.service.UmsAdminCacheService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

import static cn.deschen.eshop.user.constants.UmsAdminCacheConstant.*;

/**
 * @Author hanbin_chen
 * @Description 后台用户管理缓存
 * @DateTime 2021/2/4 13:46
 * @Version V1.0.0
 */
@Service
@Slf4j
public class UmsAdminCacheServiceImpl implements UmsAdminCacheService {

    @Autowired
    private RedisService redisService;

    @Override
    public UmsAdmin getAdminByUsername(String username) {
        String key = REDIS_DATABASE + REDIS_KEY_ADMIN + username;
        return (UmsAdmin) redisService.get(key);
    }

    @Override
    public void setAdminByUsername(String username, UmsAdmin admin) {
        String key = REDIS_DATABASE + REDIS_KEY_ADMIN + username;
        redisService.set(key, admin, REDIS_EXPIRE);
    }
    @Override
    public List<UmsResource> getResourceListById(Long adminId) {
        String key = REDIS_DATABASE + REDIS_KEY_RESOURCE_LIST + adminId;
        return (List<UmsResource>) redisService.get(key);
    }

    @Override
    public void setResourceListById(Long adminId, List<UmsResource> resourceList) {
        String key = REDIS_DATABASE + REDIS_KEY_RESOURCE_LIST + adminId;
        redisService.set(key, resourceList, REDIS_EXPIRE);
    }
}
